package comdemo.controller;


import comdemo.pojo.Result;
import comdemo.pojo.User;
import comdemo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 沈仁聪
 * @since 2025-08-19
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private IUserService userService;
    /**
     * <p>
     * 获取所有的用户
     * </p>
     *
     * @author 沈仁聪
     * @since 2025-08-19
     */
    @GetMapping
    public Result getUser() {
        List<User> list = userService.list();
        return Result.success(list);
    }

    /**
     * <p>
     * 新增用户
     * </p>
     *
     * @author 沈仁聪
     * @since 2025-08-20
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {

        userService.save(user);
        return Result.success();
    }

    /**
     * <p>
     * 通过ID更新用户信息
     * </p>
     *
     * @author 沈仁聪
     * @since 2025-08-20
     */
    @PostMapping("/update")
    public Result updateUser(@RequestBody User user) {
        userService.updateById(user);
        return Result.success();
    }

    /**
     * <p>
     * 通过ID删除用户
     * </p>
     *
     * @author 沈仁聪
     * @since 2025-08-20
     */
    @DeleteMapping("/delete")
    public Result deleteUser(@RequestBody User user) {
        userService.removeById(user);
        return Result.success();
    }
}
